#include <utility>
#include <nice2d/util.h>
#include <nice2d/widgets/abstractslider.h>

IMPLEMENT_OBJECT(AbstractSlider,Control)

class AbstractSliderData
{
public:
    int32_t value = 0;
    int32_t valueStep = 1;
    std::pair<int32_t,int32_t> range = {0,100};
};

AbstractSlider::AbstractSlider():
    data(new AbstractSliderData())
{
}

AbstractSlider::~AbstractSlider()
{
}

void AbstractSlider::setRange(int32_t low, int32_t high)
{
    if (low > high)
        return;

    data->range = std::make_pair(low,high);
    data->value = std::clamp(data->value,low,high);
}

int32_t AbstractSlider::getLow()const
{
    return data->range.first;
}

int32_t AbstractSlider::getHigh()const
{
    return data->range.second;
}

void AbstractSlider::setValue(int32_t value)
{
    if (value < data->range.first) {
        data->value = data->range.first;
        valueChanged(data->value);
        onValueChanged();
    } else if (value > data->range.second) {
        data->value = data->range.second;
        valueChanged(data->value);
        onValueChanged();
    } else if (data->value != value) {
        data->value = value;
        valueChanged(data->value);
        onValueChanged();
    }
}

int32_t AbstractSlider::getValue()const
{
    return data->value;
}

void AbstractSlider::setStep(int32_t value)
{
    data->valueStep = value;
}

int32_t AbstractSlider::getStep()const
{
    return data->valueStep;
}

float AbstractSlider::getValuePercent() const
{
    return (data->value - data->range.first) / float(data->range.second - data->range.first);
}

void AbstractSlider::onValueChanged()
{
}
